package models

type Tag struct {
	Model

	Name string `json:"name"`
	CreatedBy string `json:"created_by"`
	ModifiedBy string `json:"modified_by"`
	State int `json:"state"`
}

// GetTags 获取所有标签
func GetTags(pageNum int, pageSize int, maps interface{}) (tags []Tag)  {
	db.Where(maps).Offset(pageNum).Limit(pageSize).Find(&tags)
	return
}

// GetTagTotal 获取标签总数
func GetTagTotal(maps interface{}) (count int){
	db.Model(&Tag{}).Where(maps).Count(&count)
	return
}

// AddTag 删除标签
func AddTag(name string, state int, createBy string) bool {
	db.Create(&Tag{
		Name: name,
		State: state,
		CreatedBy: createBy,
	})
	return true
}

// ExistTagByName 获取单条标签
func ExistTagByName(name string) bool {
	var tag Tag
	db.Select("id").Where("name = ?", name).First(&tag)
	if tag.ID > 0 {
		return true
	}
	return false
}

// ExistTagByID 获取单条ID
func ExistTagByID(id int) bool {
	var tag Tag
	db.Select("id").Where("id = ?", id).First(&tag)
	if tag.ID > 0 {
		return true
	}
	return false
}

// DeleteTag 删除标签
func DeleteTag(id int) bool {
	db.Where("id = ?",id).Delete(&Tag{})
	return true
}

// EditTag 更新标签
func EditTag(id int, data interface{}) bool {
	db.Model(&Tag{}).Where("id = ?",id).Update(data)
	return true
}

